<template>
  <div class="param-main">
    <el-form ref="tblMain" :model="serachForm" labelPosition="right" inline="" label-suffix=":" label-width="110px">
      <el-row :span="24">
        <el-col :span="8">
          <el-form-item label="适用范围">
            <el-select v-model="serachForm.contentType" size="small" filterable placeholder="请选择交接项适用范围">
              <el-option v-for="item in contentTypeList" :key="item.code" :value="item.code" :label="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="getStrHandoverList()">查询</el-button>
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" @click="detailTblMain(1)">新增</el-button>
        <el-button size="small" type="danger" @click="deleteEvent(1)">删除</el-button>
      </div>
    </el-form>
    <div style="height: calc(100% - 160px)">
      <el-table ref="tblMain" style="margin-top: 20px" :data="listData" height="100%"
        @selection-change="handleSelectionChange" v-loading.body="loading" border stripe>
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column type="index" width="50" label="序号" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <el-button type="text" @click="detailTblMain(2, scope.row)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="deleteEvent(scope.row)">删除</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="detailTblMain(3, scope.row)">明细</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="handoverContent" label="交接内容" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.handoverContent }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="contentType" label="交接项适用范围" align="center">
          <template slot-scope="scope">
            <span> {{ formatterFiled(scope.row.contentType, 'code', 'name', contentTypeList) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
      </el-table>
    </div>
    <el-pagination style="margin-top: 10px" :current-page.sync="pageInfo.page" :page-size="pageInfo.size"
      :total="pageInfo.total" layout="total, sizes, prev, pager, next" @size-change="handleSizeChange"
      @current-change="handleCurrentChange"></el-pagination>
    <add-str-handover ref="addBrand" v-if="visible" @ok="saveData" @cancle="cancle" :title="title"
      :row="rowData"></add-str-handover>
  </div>
</template>
<script>
import AddStrHandover from './addStrHandover'
import {
  addStrHandover,
  delStrHandover,
  upStrHandover,
  getStrHandoverList
} from '@/api/businessCo'
export default {
  name: 'applyMain',
  props: ['pageData'],
  components: {
    AddStrHandover
  },
  data() {
    return {
      visible: false,
      loading: false,
      rowData: {},
      title: '',
      selection: [],
      listData: [],
      serachForm: {
        contentType: '',
      },
      pageInfo: {
        page: 1,
        size: 10,
        total: 0
      },
      contentTypeList: [
        {
          code: '0',
          name: '普通员工',
        },
        {
          code: '1',
          name: '技工',
        },
        {
          code: '2',
          name: '通用',
        },
      ],
    }
  },
  watch: {},
  mounted() {
    this.getStrHandoverList()
  },
  methods: {
    /**
     * 获取码值
     */
    formatterFiled(cellValue, key, val, list) {
      let item = list.find((item) => item[key] === cellValue)
      return item ? item[val] : ''
    },
    /**
     * 获取交接项列表信息
     */
    getStrHandoverList() {
      let data = {
        page: this.pageInfo.page,
        size: this.pageInfo.size,
        ...this.serachForm
      }
      this.loading = true
      getStrHandoverList(data).then((res) => {
        const { code = undefined, data = {} } = res.data
        if (!code) {
          this.loading = false
          this.listData = data.record;
          this.pageInfo.total = data.total
        } else {
          this.$message.error(data)
        }
      }).catch(() => {
        this.$message.error('服务器繁忙')
        this.loading = false
      })
    },
    /**
     * 删除
     */
    deleteEvent(row) {
      let ids = []
      if (row == '1') {
        if (this.selection.length < 1) {
          this.$message.warning('请至少选择一条数据')
          return
        }
        ids = this.selection.map(item => {
          return item.id
        })
      } else {
        ids.push(row.id)
      }
      this.$confirm('此操作将永久删除该交接项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delStrHandover(ids).then((res) => {
            const { code = undefined, data = {} } = res.data
            if (!code) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.getStrHandoverList();
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    /**
     * 点击功能按钮
     */
    detailTblMain(type, row) {
      if (type != 1) {
        this.title = type == 2 ? '修改交接项' : '交接项详情'
        this.rowData = JSON.parse(JSON.stringify(row))
        this.visible = true
      } else {
        this.title = '新增交接项'
        this.rowData = {}
        this.visible = true
      }
    },
    /**
     * 保存
     */
    saveData(val) {
      if (this.title == '新增交接项') {
        addStrHandover(val).then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getStrHandoverList();
            this.cancle()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!'
            })
          }
        })
      } else {
        upStrHandover(val).then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getStrHandoverList();
            this.cancle()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!'
            })
          }
        })
      }
    },
    /**
     * 关闭弹框
     */
    cancle() {
      this.visible = false
    },
    /**
    * 多选
    */
    handleSelectionChange(val) {
      this.selection = val
    },
    /**
    * 重置
    */
    dataReset() {
      this.pageInfo.page = 1
      this.pageInfo.size = 10
      this.serachForm.contentType = ''
      this.getStrHandoverList()
    },
    /**
    * 分页
    */
    handleSizeChange(val) {
      this.pageInfo.size = val
      this.getStrHandoverList()
    },
    /**
     * 分页
     */
    handleCurrentChange(val) {
      this.pageInfo.page = val
      this.getStrHandoverList()
    },
  }
}
</script>
<style scoped lang="scss"></style>
